package org.sojex.photo.data;
/*
 * 解析xml
 * */
import java.net.URL;
import java.util.Calendar;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.sojex.photo.bean.CookBean;
import org.sojex.photo.bean.CookBook;
import org.sojex.photo.bean.Cooks;
import org.sojex.photo.manage.DBManager;
import org.sojex.photo.util.GlobalData;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class DataAction {
	public final static int NET_RST = 0;
	public final static int ERR = 500;
	String url;
	Handler handler;
	Context context;
	DBManager db;

	public DataAction(Handler handler, String url,Context context) {
		// TODO Auto-generated constructor stub
		this.url = url;
		this.handler = handler;
		this.context = context;
		db = DBManager.open(context);
		getDataFromNetWork();
	}

	public void getDataFromNetWork() {
		try {
			URL _url = new URL(url);
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xml = sp.getXMLReader();
			PriceHandler priceHandler = new PriceHandler();
			xml.setContentHandler(priceHandler);
			InputSource source = new InputSource(_url.openStream());
			xml.parse(source);
			Message msg = new Message();
			msg.what = NET_RST;
			msg.obj = priceHandler.getResult();// 解析成功，返回数据
			handler.sendMessage(msg);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Message msg = new Message();
			msg.what = ERR;
			msg.obj = e.toString();
			handler.sendMessage(msg);
		}
	}

	class PriceHandler extends DefaultHandler {
		CookBook cookBook;
		Cooks cooks;
		CookBean cookBean;
		String current = null;

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			// TODO Auto-generated method stub
			if (localName.equals("cook_book"))
				cookBook = new CookBook();
			if (localName.equals("cook_suit"))
				cooks = new Cooks();
			if (localName.equals("kind"))
				cookBean = new CookBean();
			current = localName;
		}

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			// TODO Auto-generated method stub
			if (current != null) {
				String val = new String(ch, start, length);
				if (current.equals("suit_name"))
					cooks.cooksName = val;
				if (current.equals("name"))
					cookBean.name = val;
				if(current.equals("suit_url"))
					cooks.cooksUrl = val;
				if (current.equals("url"))
					cookBean.url = val;
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			// TODO Auto-generated method stub
			if (localName.equals("kind"))
				cooks.cooks.add(cookBean);
			if (localName.equals("cook_suit"))
				cookBook.cookBook.add(cooks);
			current = null;
		}

		public CookBook getResult() {
			GlobalData.cookBook = cookBook.cookBook;
			db.insertToCache(cookBook.id()+"", cookBook.BeanToString(), Calendar.getInstance().getTimeInMillis()+"");
			return cookBook;
		}
	}
}
